
import time

from selenium import webdriver
from selenium.webdriver import Chrome
# 引入By类
from selenium.webdriver.common.by import By
# 引入expected_conditions类，并重命名为EC
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select
# 引入WebDriverWait
from selenium.webdriver.support.ui import WebDriverWait

#火狐
# web=Firefox()
# web = Edge()
#谷歌
web = Chrome()
web.maximize_window()
options = webdriver.ChromeOptions()
#等待框架
wait = WebDriverWait(web, 30, 0.5)
url = "https://www.qschina.cn/university-rankings/university-subject-rankings/2022/arts-humanities"
web.get(url)
web.execute_script("window.scrollTo(document.body.scrollHeight,0)")
#等待元素加载
wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="qs-rankings-datatables"]/div[1]/ul/li[2]/a')))
time.sleep(5)
#点击排名指标
elem = web.find_element_by_xpath('//*[@id="qs-rankings-datatables"]/div[1]/ul/li[2]/a')
elem.click()
#点击同意按钮
popup = web.find_element_by_css_selector("div#popup-buttons").find_element(by=By.CLASS_NAME, value="agree-button")
popup.click()
web.execute_script("window.scrollTo(0,3300)")

wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "select[name='qs-rankings-indicators_length']")))
elem = web.find_element_by_css_selector("select[name='qs-rankings-indicators_length']")
web.execute_script("arguments[0].removeAttribute(arguments[1])", elem, 'class')
Select(elem).select_by_value("-1")

opts = web.find_elements(by=By.XPATH,
                         value='//*[@id="qs-rankings-indicators_wrapper"]/div[2]/div[3]/div[2]/div/table/tbody/tr')
for opt in opts:  # 每个按钮都点击，比较下流的操作
    # get大学相关的url
    h = opt.find_element_by_css_selector('a.uni-link')
    herf = h.get_attribute('href')
    print(herf)
